package com.example.administrator.myapplication.Http;


import android.util.Log;
import com.example.administrator.myapplication.Bean.AjaxResult;
import com.example.administrator.myapplication.Bean.DuanziBean;
import com.example.administrator.myapplication.Bean.Label;
import com.example.administrator.myapplication.Bean.Notice;
import com.example.administrator.myapplication.Bean.Picture;
import com.example.administrator.myapplication.Bean.User;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2019/6/14 0014.
 * 使用动态代理实例化RetrofitService
 */

public class RetrofitHelper {
    private static OkHttpClient okHttpClient;
    private RetrofitService retrofitService;


    public RetrofitHelper(String host){
        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .create();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(host)
                .client(getOkHttpClient())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
        retrofitService = retrofit.create(RetrofitService.class);
    }

    /**
     * 取得okhttpclient
     * 单例
     * */
    public static OkHttpClient getOkHttpClient(){
        if(okHttpClient==null){
            Log.e("msg","new a okhttp client!");
            okHttpClient = new OkHttpClient.Builder()
                    .cookieJar(new CookieManger())
                    .addInterceptor(new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            Request request = chain.request()
                                    .newBuilder()
                                    .removeHeader("User-Agent")//移除旧的
                                    .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36")//添加真正的头部
                                    .build();
                            return chain.proceed(request);
                        }
                    })
                    .retryOnConnectionFailure(true)
                    .connectTimeout(30, TimeUnit.SECONDS)
                    .build();
        }
        return okHttpClient;
    }

    /**
     * 向Model提供可调用的API接口
     * */
    public Call<DuanziBean> getDuanzi(int page){
        return retrofitService.getDuanzi(page,10,"image");
    }
    public Call<AjaxResult>login(String username, String password, String validateCode, boolean rememberMe){
        return retrofitService.login(username,password,validateCode,rememberMe);
    }
    public Call<AjaxResult>logon(User user){
        return  retrofitService.logon(user);
    }
    public Call<ResponseBody>checkLoginNameUnique(String loginName){
        return  retrofitService.checkLoginNameUnique(loginName);
    }
    public Call<ResponseBody>checkPhoneUnique(String phonenumber){
        return  retrofitService.checkPhoneUnique(phonenumber);
    }
    public Call<ResponseBody>checkEmailUnique(String email){
        return  retrofitService.checkEmailUnique(email);
    }
    public Call<User>getProfile(){
        return retrofitService.getProfile();
    }
    public Call<AjaxResult>updateProfile(User  user){
        return retrofitService.updateProfile(user.getLoginName(),user.getUserName(),user.getEmail(),user.getPhonenumber(),user.getSex(),user.getPassword(),user.getAvatar(),user.getUserId());
    }
    public Call<Picture>getRamdomPicture(){
        return retrofitService.getRandomPicture();
    }
    public Call<AjaxResult>addLabel(Label label){
        return retrofitService.addLabel(label);
    }
    public Call<List<HashMap>>getLabels(){
        return retrofitService.getLabels();
    }
    public Call<List<Notice>>getNotices(Notice notice){
        return retrofitService.getNotices(notice);
    }
}
